package com.iskny.framework.session;

import java.util.HashMap;
import java.util.Map;
/**
 * 管理session数据,需改进
 * threadLocalMaps换为Thread和ISessionManager的映射
 * @author 伟强
 *
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class SessionDataKit {

	private static boolean inited = false;
//	private static final Set<String> keySets = new HashSet<String>();
	private static final Map<String,Class<? extends ISessionManager<?>>> manageMaps = new HashMap<String, Class<? extends ISessionManager<?>>>();
	private static final Map<String,ThreadLocal<ISessionManager>> threadLocalMaps = new HashMap<String, ThreadLocal<ISessionManager>>();
	static{init();}//可以在插件中调用
	static void init(){
		manageMaps.put(UserSession.dataKey, UserSession.class);
		inited = true;
	}
	public static void set(String key,ISessionManager value){
		ThreadLocal<ISessionManager> manager = new ThreadLocal<ISessionManager>();
		threadLocalMaps.put(key, manager);
		manager.set(value);
	}
	public static void setAttr(String key,Object value){
		checkInited();
		ThreadLocal<ISessionManager> lo = threadLocalMaps.get(key);
		if(lo==null){
			throw new SessionDataException("key无效，请使用ISessionManager.dataKey静态常量来获取key参数");
		}
		ISessionManager manager = lo.get();
		manager.setAttr(value);
	}
	public static ISessionManager get(String key){
		return threadLocalMaps.get(key).get();
	}
	public static void checkInited(){
		if(!inited)throw new SessionDataException("未初始化");
	}
	
}
